package com.gitee.huanminabc.utils_test.service;

import com.gitee.huanminabc.dynamicdb.aop.DBSwitch;
import com.gitee.huanminabc.dynamicdb.aop.DBTransactional;
import com.gitee.huanminabc.utils_test.dal.dao.TUserDatasourceManager;
import com.gitee.huanminabc.utils_test.dal.dao.UserDaoService;
import com.gitee.huanminabc.utils_test.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: java-huanmin-utils
 * @description: 用户
 * @author: huanmin
 * @create: 2024-12-10 10:28
 **/
@Service

public class UserService {
    @Autowired
    private UserDaoService userDaoService;
    @Autowired
    private TUserDatasourceManager tUserDatasourceManager;

    //多数据源事务嵌套内置的事务
    @DBTransactional
    public void updateByTran2(int id,String name) {

        tUserDatasourceManager.updateByTran();
        System.out.println("====================0");


        //这个用的是resource数据源
        UserEntity byId = userDaoService.getById(id);
        byId.setNickName(name);
        userDaoService.updateById(byId.getId(),name);

        System.out.println("====================1");

        UserEntity byId1 = userDaoService.getById(2038472708);
        byId1.setNickName(name);
        userDaoService.updateById(byId1.getId(), name);
        System.out.println("====================2");


        int a=1/0;
    }


    @DBSwitch("resource")
    public void updateById(int id) {

    }

    //多数据源事务嵌套内置的事务
    @DBTransactional
//    @DBSwitch("resource")
//    @Transactional
    public void updateByTran1(int id,String name) {
        //这个用的是默认数据源 , 内部没有事务
        tUserDatasourceManager.updateByTran();
        System.out.println("====================0");

        //这个用的是resource数据源
        UserEntity byId = userDaoService.getById(id);
        byId.setNickName(name);
        //内部嵌套了事务@Transaction
        userDaoService.updateById(byId.getId(),name);

        System.out.println("====================1");

        UserEntity byId1 = userDaoService.getById(2038472708);
        byId1.setNickName(name);
        userDaoService.updateById(byId1.getId(), name);
        System.out.println("====================2");

//        int a=1/0;
    }

}
